package org.dromara.pe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.pe.domain.DeviceScoreRaw;
import org.dromara.pe.domain.bo.DeviceScoreRawBo;
import org.dromara.pe.domain.vo.DeviceScoreRawVo;
import org.dromara.pe.mapper.DeviceScoreRawMapper;
import org.dromara.pe.service.IDeviceScoreRawService;

import org.springframework.stereotype.Service;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-25
 */
@RequiredArgsConstructor
@Service
public class DeviceScoreRawServiceImpl implements IDeviceScoreRawService {

    private final DeviceScoreRawMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public DeviceScoreRawVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<DeviceScoreRawVo> queryPageList(DeviceScoreRawBo bo, PageQuery pageQuery) {
        QueryWrapper<DeviceScoreRaw> lqw = buildQueryWrapper(bo);
        Page<DeviceScoreRawVo> result = baseMapper.selectPageDeviceScoreRawList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<DeviceScoreRawVo> queryList(DeviceScoreRawBo bo) {
        QueryWrapper<DeviceScoreRaw> lqw = buildQueryWrapper2(bo);
        return baseMapper.selectPageDeviceScoreRawList(lqw);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<DeviceScoreRawVo> queryInfoList(DeviceScoreRawBo bo) {
        QueryWrapper<DeviceScoreRaw> lqw = buildQueryWrapper3(bo);
        return baseMapper.selectPageDeviceScoreRawList(lqw);
    }

    private QueryWrapper<DeviceScoreRaw> buildQueryWrapper(DeviceScoreRawBo bo) {
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Map<String, Object> params = bo.getParams();
        try {
            if(params.get("beginCompletedTime") != null && params.get("endCompletedTime") != null){
                params.put("beginCompletedTime",dateFormat.parse(params.get("beginCompletedTime").toString()+" 00:00:00"));
                params.put("endCompletedTime",dateFormat.parse(params.get("endCompletedTime").toString()+" 23:59:59"));

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        QueryWrapper<DeviceScoreRaw> lqw = new QueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(bo.getUid()), "pdsr.uid", bo.getUid());
        lqw.eq(StringUtils.isNotBlank(bo.getSportId()), "pdsr.sport_id", bo.getSportId());
        lqw.eq(StringUtils.isNotBlank(bo.getAcademicYear()), "pdsr.stu_year", bo.getAcademicYear());
        lqw.like(StringUtils.isNotBlank(bo.getName()), "pdsr.name", bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getSportName()), "pdsr.sport_name", bo.getSportName());
        // lqw.eq(bo.getClassId() != null, "pmi.class_id", bo.getClassId());
        lqw.eq(bo.getClassId() != null, "pdsr.class_id", bo.getClassId());
        lqw.eq(bo.getTaskId() != null, "pdsr.task_id", bo.getTaskId());
        lqw.eq(bo.getType() != null, "pdsr.type", bo.getType());
        lqw.in(CollectionUtils.isNotEmpty(bo.getScoreTypes()) , "pdsr.score_type", bo.getScoreTypes());
        lqw.eq(bo.getScoreType()!=null, "pdsr.score_type", bo.getScoreType());
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), "pdsr.level", bo.getLevel());
        if(StringUtils.isEmpty(bo.getOrderByColumn())){
            lqw.orderBy(true,false,"pdsr.completed_time");
        }
        lqw.orderBy(true,true,"pdsr.uid");

        lqw.between(params.get("beginCompletedTime") != null && params.get("endCompletedTime") != null,
            "pdsr.completed_time",params.get("beginCompletedTime"), params.get("endCompletedTime"));
        return lqw;
    }
    private QueryWrapper<DeviceScoreRaw> buildQueryWrapper2(DeviceScoreRawBo bo) {
        Map<String, Object> params = bo.getParams();
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<DeviceScoreRaw> lqw = new QueryWrapper<>();
        try {
            if(params.get("beginCompletedTime") != null && params.get("endCompletedTime") != null){
                params.put("beginCompletedTime",dateFormat.parse(params.get("beginCompletedTime").toString()+" 00:00:00"));
                params.put("endCompletedTime",dateFormat.parse(params.get("endCompletedTime").toString()+" 23:59:59"));

            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        lqw.eq(StringUtils.isNotBlank(bo.getUid()), "pdsr.uid", bo.getUid());
        lqw.eq(StringUtils.isNotBlank(bo.getSportId()), "pdsr.sport_id", bo.getSportId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), "pdsr.name", bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getSportName()), "pdsr.sport_name", bo.getSportName());
        lqw.eq(bo.getClassId() != null, "pmi.class_id", bo.getClassId());
        lqw.eq(bo.getType() != null, "pdsr.type", bo.getType());
        lqw.eq(bo.getScoreType() != null, "pdsr.score_type", bo.getScoreType());
        lqw.eq(StringUtils.isNotBlank(bo.getAcademicYear()), "pdsr.stu_year", bo.getAcademicYear());
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), "pdsr.level", bo.getLevel());
        lqw.in(CollectionUtils.isNotEmpty(bo.getScoreTypes()) , "pdsr.score_type", bo.getScoreTypes());
        lqw.eq(bo.getScoreType()!=null, "pdsr.score_type", bo.getScoreType());
        if(StringUtils.isEmpty(bo.getOrderByColumn())){
            lqw.orderBy(true,false,"pdsr.completed_time");
        }else{
            lqw.orderBy(StringUtils.isNotEmpty(bo.getIsAsc()),"asc".equals(bo.getIsAsc()),"pdsr.completed_time");
        }
        lqw.orderBy(true,true,"pdsr.uid");
        lqw.between(params.get("beginCompletedTime") != null && params.get("endCompletedTime") != null,
            "pdsr.completed_time",params.get("beginCompletedTime"), params.get("endCompletedTime"));

        return lqw;
    }
    private QueryWrapper<DeviceScoreRaw> buildQueryWrapper3(DeviceScoreRawBo bo) {
        Map<String, Object> params = bo.getParams();
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<DeviceScoreRaw> lqw = new QueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(bo.getUid()), "pdsr.uid", bo.getUid());
        lqw.eq(StringUtils.isNotBlank(bo.getSportId()), "pdsr.sport_id", bo.getSportId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), "pdsr.name", bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getSportName()), "pdsr.sport_name", bo.getSportName());
        lqw.eq(bo.getClassId() != null, "pmi.class_id", bo.getClassId());
        lqw.eq(bo.getType() != null, "pdsr.type", bo.getType());
        lqw.eq(bo.getScoreType() != null, "pdsr.score_type", bo.getScoreType());
        lqw.eq(StringUtils.isNotBlank(bo.getAcademicYear()), "pdsr.stu_year", bo.getAcademicYear());
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), "pdsr.level", bo.getLevel());
        lqw.in(CollectionUtils.isNotEmpty(bo.getScoreTypes()) , "pdsr.score_type", bo.getScoreTypes());
        lqw.eq(bo.getScoreType()!=null, "pdsr.score_type", bo.getScoreType());
        if(StringUtils.isEmpty(bo.getOrderByColumn())){
            lqw.orderBy(true,false,"pdsr.completed_time");
        }else{
            lqw.orderBy(StringUtils.isNotEmpty(bo.getIsAsc()),"asc".equals(bo.getIsAsc()),"pdsr.completed_time");
        }
        lqw.orderBy(true,true,"pdsr.uid");
        lqw.between(params.get("beginCompletedTime") != null && params.get("endCompletedTime") != null,
            "pdsr.completed_time",params.get("beginCompletedTime"), params.get("endCompletedTime"));
        if(bo.getBeginCompletedTime() != null && bo.getEndCompletedTime() != null){
            String beginCompletedTime = dateFormat.format(new Date(bo.getBeginCompletedTime()));
            String endCompletedTime = dateFormat.format(new Date(bo.getEndCompletedTime()));
            lqw.between(bo.getBeginCompletedTime() != null && bo.getEndCompletedTime() != null,
                "pdsr.completed_time",beginCompletedTime, endCompletedTime);
        }
        if(bo.getLimit()==null){
            lqw.last("limit 500");
        }else if(bo.getLimit()!=null&&bo.getLimit()>0&&bo.getLimit()<=1000){
            lqw.last("limit "+bo.getLimit());
        }else {
            lqw.last("limit 1000");
        }

        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(DeviceScoreRawBo bo) {
        DeviceScoreRaw add = MapstructUtils.convert(bo, DeviceScoreRaw.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(DeviceScoreRawBo bo) {
        DeviceScoreRaw update = MapstructUtils.convert(bo, DeviceScoreRaw.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DeviceScoreRaw entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
